<?php
$page = 'Team';
$in_site = 'dassnfu';
$loginVereist = 1;
include_once($_SERVER['DOCUMENT_ROOT']."/start.php");

//Mogen de rechten gewijzigd worden?
$wijzigen_toegestaan = false;
if(($_SESSION['rechten'] & (1 << $rechten['spelersrechten_wijzigen'])) > 0) {
	$wijzigen_toegestaan = true;	
}
/*
//Heb je wel het recht om deze pagina te bekijken?
if(($_SESSION['rechten'] & (1 << $rechten['spelersrechten_bekijken'])) == 0) {
	include_once($_SERVER['DOCUMENT_ROOT']."/header.php");
	include_once($_SERVER['DOCUMENT_ROOT']."/sidebar.php");
	echo "<div id='main'><div id='submain'><div class='content'><div class='subcontent'>
		<div class='content_header'><h1>Spelers rechten</h1></div>
		<div class='content_main'>";
	echo "<div class='error'><p class='icon'>Je hebt niet het recht om deze pagina te bekijken.</p></div>";
	echo $_SESSION['rechten'];
	echo "</div></div></div></div></div>";	//content_main, sub_content, content, sub_main, main
	include_once($_SERVER['DOCUMENT_ROOT']."/footer.php");
	die();
}
*/
include_once($_SERVER['DOCUMENT_ROOT']."/php_functions/my_query.php");

if(isset($_GET['wijzigen'])) {
	//Heb je wel het recht om rechten te wijzigen?
	if($wijzigen_toegestaan == false) {
		include_once($_SERVER['DOCUMENT_ROOT']."/header.php");
		include_once($_SERVER['DOCUMENT_ROOT']."/sidebar.php");
		echo "<div id='main'><div id='submain'><div class='content'><div class='subcontent'>
			<div class='content_header'><h1>Spelers rechten</h1></div>
			<div class='content_main'>";
		echo "<div class='error'><p class='icon'>Je hebt niet het recht om deze pagina te bekijken.</p></div>";
		echo $_SESSION['rechten'];
		echo "</div></div></div></div></div>";	//content_main, sub_content, content, sub_main, main
		include_once($_SERVER['DOCUMENT_ROOT']."/footer.php");
		die();
	}else {
		if(empty($_GET['team']) || empty($_GET['spelerId']) || !isset($_GET['functie'])) {
			$error .= "<div class='error'><p class='icon'>Er is geen team, speler en/of functie geselecteerd</p></div>";
		}
		if(empty($error)) {
			$myQuery->resetValues();
			$myQuery->tabel = "Teambeheer";
			$myQuery->attributen = "Teamgenoten.teambeheerId, Teamgenoten.rechten";
			$myQuery->spelerId = $mainClass->spelerId;
			$myQuery->teamId = $_GET['team'];
			$myQuery->rechtenNu = $rechten['spelersrechten_wijzigen'];
			$myQuery->alleTeamgenoten = true;
			$myQuery->voorwaarde = "Teambeheer.teamId = :team AND Teamgenoten.spelerId = :teamspeler AND Teamgenoten.teamId = :team";
			$myQuery->parameters = array(
				'1' => array('name' => 'team',			'value' => $_GET['team'],		'type' => PDO::PARAM_STR),
				'2' => array('name' => 'teamspeler',	'value' => $_GET['spelerId'],	'type' => PDO::PARAM_INT));
			$myQuery->makeSelectQuery(true);
			//$myQuery->printQuery();
			if(!$myQuery->isValidResult()) {
				$mainClass->addError("Je hebt niet het recht om deze wijziging door te voeren.");
				$mainClass->allesGoed = false;
			}
			
			if($mainClass->allesGoed) {
				$myQuery->resetValues();
				$myQuery->query = "UPDATE Teambeheer SET rechten = CASE WHEN (rechten & ".pow(2, $_GET['functie']).") >= 1 THEN rechten - ".pow(2, $_GET['functie'])." ELSE rechten + ".pow(2, $_GET['functie'])." END WHERE spelerId = :spelerId AND teamId = :team AND van <= NOW() AND (tot >= NOW() OR tot IS NULL)";
				$myQuery->parameters = array(
							'1' => array('name' => 'spelerId',	'value' => $_GET['spelerId'],	'type' => PDO::PARAM_INT),
							'2' => array('name' => 'team',		'value' => $_GET['team'],		'type' => PDO::PARAM_INT));
				if(!$myQuery->doQuery()) {
					$mainClass->addError("Er iets mis gegaan tijdens het versturen van de gegevens.");
					$mainClass->allesGoed = false;
				}else {
					$mainClass->addSucces("De wijziging is doorgevoerd.");
				}
			}
		}
	}
}
$mainClass->allesGoed = true;
$bestanden = "<link rel='stylesheet' type='text/css' href='/bestanden/css/style_tabellen".$style_nmr.".css' />
<!-- Popup -->
<link rel='stylesheet' type='text/css' href='/bestanden/js/popup/css/popup".$style_nmr.".css' />
<script type='text/javascript' src='/bestanden/js/popup/js/popup.js'></script>
<!-- Qtip -->
<link rel='stylesheet' type='text/css' href='/bestanden/js/qtip/css/jquery.qtip".$style_nmr.".css' />
<script type='text/javascript' src='/bestanden/js/qtip/js/jquery.qtip.min.js'></script>
<script type='text/javascript' src='/bestanden/js/qtip/js/standard_initialize.js'></script>
<script type='text/javascript'>
$(document).ready(function() {
//Horizontale en verticale hover
$('table').delegate('td','mouseover mouseleave', function(e) {
   	if (e.type == 'mouseover' && $('colgroup').eq($(this).index()).hasClass('noHover') == false) {
   		$(this).parent().addClass('highlight');
      	$('colgroup').eq($(this).index()).addClass('highlight');
    }else {
      	$(this).parent().removeClass('highlight');
      	$('colgroup').eq($(this).index()).removeClass('highlight');
    }
});";
if($wijzigen_toegestaan) {
	$bestanden .=  "//De link voor het wijzigen
	$('.link_trigger').click(function(){
		console.log('ja');
		var value = $(this).attr('rel').split('-');
		window.location = './spelersrechten.php?wijzigen=1&team=' + value[0] + '&functie=' + value[1] + '&spelerId=' + value[2];
	});";
}
$bestanden .= "})</script>
<style type='text/css'>
.my_padding tr td, .my_padding tr th {
	padding: 0px 7px;
}
</style>";

include_once($_SERVER['DOCUMENT_ROOT']."/php_functions/instellingen.php");
include_once($_SERVER['DOCUMENT_ROOT']."/header.php");
include_once($_SERVER['DOCUMENT_ROOT']."/sidebar.php");

echo $mainClass->showPageStart(false);
echo $mainClass->showSelectiesStart(true);
	if(empty($_GET['team'])) {	$team = 0;
	}else {						$team = $_GET['team'];}
	//Team
	$myQuery->resetValues();
	$myQuery->tabel = "Team";
	$myQuery->distinct = "Team.teamId";
	$myQuery->attributen = "Team.team";
	$myQuery->spelerId = $mainClass->spelerId;
	$myQuery->rechtenNu = $rechten['spelersrechten_bekijken'];
	$myQuery->voorwaarde = "Teambeheer.teamId = Team.teamId";
	$myQuery->orderBy = "Team.team";
	$myQuery->makeSelectQuery(true);
	$team = team_selecteren($myQuery->result, "./spelersrechten.php?", true, $team);
	if($team == -1) {	$mainClass->addError("Je mag geen teamrechten bekijken.");	$mainClass->allesGoed = false;}
	
echo $mainClass->showErrors();
echo $mainClass->showSelectiesEnd();

if($mainClass->allesGoed) {
	/*
	 * Rechten van de spelers
	 */
	
	//Spelers ophalen die dit seizoen bij het team hebben gespeeld
	/*$result = mysql_query("SELECT Teamgenoot.*, BIT_OR(Teamgenoot.rechten) AS rechten1, Speler.spelerId, Speler.voornaam, Speler.tussenvoegsel, Speler.achternaam
		FROM Teambeheer, Teambeheer AS Teamgenoot, Speler
		WHERE Teambeheer.teamId = ".$team."
			AND Teambeheer.spelerId = ".$_SESSION['spelerId']." AND Teambeheer.rechten & (b'1' << ".$rechten['rechten_bekijken'].") AND Teambeheer.van <= NOW() AND (Teambeheer.tot >= NOW() OR Teambeheer.tot IS NULL)
				AND Teamgenoot.teamId = ".$team." AND YEAR(Teamgenoot.van) <= ".date('Y')." AND (YEAR(Teamgenoot.tot) >= ".date('Y')." OR Teamgenoot.tot IS NULL)
				AND Speler.spelerId = Teamgenoot.spelerId
		GROUP BY Speler.spelerId
		ORDER BY Teambeheer.spelendLid DESC, Teambeheer.teamfunctie, Speler.achternaam, Speler.voornaam, Speler.tussenvoegsel, Speler.spelerId");*/
	$myQuery->resetValues();
	$myQuery->tabel = "Speler";
	$myQuery->attributen = "Teamgenoten.*, BIT_OR(Teamgenoten.rechten) AS rechten1, Speler.spelerId, Speler.voornaam, Speler.tussenvoegsel, Speler.achternaam";
	$myQuery->tabelVoorJoin = "Teambeheer";
	$myQuery->spelerId = $mainClass->spelerId;
	$myQuery->rechtenNu = $rechten['spelersrechten_bekijken'];
	$myQuery->alleTeamgenoten = true;
	$myQuery->getSpelerNaam = true;
	$myQuery->voorwaarde = "Teambeheer.teamId = :team";
	$myQuery->groupBy = "Speler.spelerId";
	$myQuery->orderBy = "Speler.achternaam, Speler.voornaam, Speler.tussenvoegsel, Speler.spelerId";
	$myQuery->parameters = array(
		'1' => array('name' => 'team', 'value' => $team, 'type' => PDO::PARAM_INT));
	$myQuery->makeSelectQuery(true);
	if(!$myQuery->isValidResult()) {
		$mainClass->placeError("Het ophalen van de gegevens is mislukt.", true);
		$mainClass->allesGoed = false;
	}
	//$myQuery->printQuery();
	
	if($mainClass->allesGoed = true) {
		$color = false;
		
		//Maak de tabel aan
		echo "<table class='content my_padding'>";
		echo "<colgroup class='noHover'></colgroup>";
		for($i = 0; $i < 20; $i++) {
			echo "<colgroup></colgroup>";
		}
		echo "<head>";
			echo "<tr class='normaal".($color ? " alternate" : "")."'>";
				echo "<th class='col_naam'>Naam</th>";
				echo "<th class='tooltip' title='Wedstrijden toevoegen/aanpassen'><span class='icon_font'>&#x27;</span></th>";					//0
				echo "<th class='tooltip' title='Trainingen/aciviteiten toevoegen/aanpassen'><span class='icon_font'>&#x2b;</span></th>";		//1
				echo "<th class='tooltip' title='Afmeldingen bekijken'><span class='icon_font'>&#x26;</span></th>";								//2
				echo "<th class='tooltip' title='Afmeldingen beheren'><span class='icon_font'>&#x26;</span></th>";								//3
				echo "<th class='tooltip' title='Scheidsrechters toevoegen/aanpassen'><span class='icon_font'>&#x33;</span></th>";				//4
				echo "<th class='tooltip' title='Kantinediensten toevoegen/aanpassen'><span class='icon_font'>&#x38;</span></th>";				//5
				echo "<th class='tooltip' title='Statistieken bekijken'><span class='icon_font'>&#x21;</span></th>";							//6
				echo "<th class='tooltip' title='Statistieken toevoegen/aanpassen'><span class='icon_font'>&#x21;</span></th>";					//7
				echo "<th class='tooltip' title='Line-up pas bekijken na de wedstrijd'><span class='icon_font'>&#x4a;</span></th>";				//8
				echo "<th class='tooltip' title='Line-up toevoegen/aanpassen'><span class='icon_font'>&#x4a;</span></th>";						//9
				echo "<th class='tooltip' title='Wissels bekijken'><span class='icon_font'>&#x39;</span></th>";									//10
				echo "<th class='tooltip' title='Wissels beheren'><span class='icon_font'>&#x39;</span></th>";									//11
				echo "<th class='tooltip' title='Andermans berichten aanpassen'><span class='icon_font'>&#x23;</span></th>";					//12
				echo "<th class='tooltip' title='Forum aanpassen (forummoderator)'><span class='icon_font'>&#x24;</span></th>";					//13
				echo "<th class='tooltip' title='Afmeldingen wedstrijden ontvangen'><span class='icon_font'>&#x26;</span></th>";				//14
				echo "<th class='tooltip' title='Afmeldingen trainingen ontvangen'><span class='icon_font'>&#x26;</span></th>";					//15
				echo "<th class='tooltip' title='Afmeldingen activiteiten ontvangen'><span class='icon_font'>&#x26;</span></th>";				//16
				echo "<th class='tooltip' title='Teamspelers beheren'><span class='icon_font'>&#x25;</span></th>";								//17
				echo "<th class='tooltip' title='Teaminstellingen bekijken'><span class='icon_font'>&#x37;</span></th>";						//18
				echo "<th class='tooltip' title='Teaminstellingen wijzigen'><span class='icon_font'>&#x37;</span></th>";						//19
				echo "<th class='tooltip' title='Teamrechten van spelers bekijken'><span class='icon_font'>&#x37;</span></th>";					//20
				echo "<th class='tooltip' title='Teamrechten van spelers wijzigen'><span class='icon_font'>&#x37;</span></th>";					//21
			echo "</tr>";
		echo "</head>";
		echo "<body>";
			$i = 0;
			($color ? $color = false : $color = true);
			while($row = $myQuery->fetchrow()) {
				$i = 0;
				echo "<tr class='normaal".($color ? " alternate" : "")."'>";
					echo "<td class='col_naam'>".$row['voornaam']." ".(isset($row['tussenvoegsel']) ? $row['tussenvoegsel']." " : "").$row['achternaam']."</td>";
					echo "<td><span class='icon_font".($wijzigen_toegestaan ? " selectable link_trigger' rel='".$team."-".$i."-".$row['spelerId']."'" : "'").">".($row['rechten1'] & (1 << $i) ? "&#x44;" : "&#x46;")."</span></td>";	$i++;
					echo "<td><span class='icon_font".($wijzigen_toegestaan ? " selectable link_trigger' rel='".$team."-".$i."-".$row['spelerId']."'" : "'").">".($row['rechten1'] & (1 << $i) ? "&#x44;" : "&#x46;")."</span></td>";	$i++;
					echo "<td><span class='icon_font".($wijzigen_toegestaan ? " selectable link_trigger' rel='".$team."-".$i."-".$row['spelerId']."'" : "'").">".($row['rechten1'] & (1 << $i) ? "&#x44;" : "&#x46;")."</span></td>";	$i++;
					echo "<td><span class='icon_font".($wijzigen_toegestaan ? " selectable link_trigger' rel='".$team."-".$i."-".$row['spelerId']."'" : "'").">".($row['rechten1'] & (1 << $i) ? "&#x44;" : "&#x46;")."</span></td>";	$i++;
					echo "<td><span class='icon_font".($wijzigen_toegestaan ? " selectable link_trigger' rel='".$team."-".$i."-".$row['spelerId']."'" : "'").">".($row['rechten1'] & (1 << $i) ? "&#x44;" : "&#x46;")."</span></td>";	$i++;
					echo "<td><span class='icon_font".($wijzigen_toegestaan ? " selectable link_trigger' rel='".$team."-".$i."-".$row['spelerId']."'" : "'").">".($row['rechten1'] & (1 << $i) ? "&#x44;" : "&#x46;")."</span></td>";	$i++;
					echo "<td><span class='icon_font".($wijzigen_toegestaan ? " selectable link_trigger' rel='".$team."-".$i."-".$row['spelerId']."'" : "'").">".($row['rechten1'] & (1 << $i) ? "&#x44;" : "&#x46;")."</span></td>";	$i++;
					echo "<td><span class='icon_font".($wijzigen_toegestaan ? " selectable link_trigger' rel='".$team."-".$i."-".$row['spelerId']."'" : "'").">".($row['rechten1'] & (1 << $i) ? "&#x44;" : "&#x46;")."</span></td>";	$i++;
					echo "<td><span class='icon_font".($wijzigen_toegestaan ? " selectable link_trigger' rel='".$team."-".$i."-".$row['spelerId']."'" : "'").">".($row['rechten1'] & (1 << $i) ? "&#x44;" : "&#x46;")."</span></td>";	$i++;
					echo "<td><span class='icon_font".($wijzigen_toegestaan ? " selectable link_trigger' rel='".$team."-".$i."-".$row['spelerId']."'" : "'").">".($row['rechten1'] & (1 << $i) ? "&#x44;" : "&#x46;")."</span></td>";	$i++;
					echo "<td><span class='icon_font".($wijzigen_toegestaan ? " selectable link_trigger' rel='".$team."-".$i."-".$row['spelerId']."'" : "'").">".($row['rechten1'] & (1 << $i) ? "&#x44;" : "&#x46;")."</span></td>";	$i++;
					echo "<td><span class='icon_font".($wijzigen_toegestaan ? " selectable link_trigger' rel='".$team."-".$i."-".$row['spelerId']."'" : "'").">".($row['rechten1'] & (1 << $i) ? "&#x44;" : "&#x46;")."</span></td>";	$i++;
					echo "<td><span class='icon_font".($wijzigen_toegestaan ? " selectable link_trigger' rel='".$team."-".$i."-".$row['spelerId']."'" : "'").">".($row['rechten1'] & (1 << $i) ? "&#x44;" : "&#x46;")."</span></td>";	$i++;
					echo "<td><span class='icon_font".($wijzigen_toegestaan ? " selectable link_trigger' rel='".$team."-".$i."-".$row['spelerId']."'" : "'").">".($row['rechten1'] & (1 << $i) ? "&#x44;" : "&#x46;")."</span></td>";	$i++;
					echo "<td><span class='icon_font".($wijzigen_toegestaan ? " selectable link_trigger' rel='".$team."-".$i."-".$row['spelerId']."'" : "'").">".($row['rechten1'] & (1 << $i) ? "&#x44;" : "&#x46;")."</span></td>";	$i++;
					echo "<td><span class='icon_font".($wijzigen_toegestaan ? " selectable link_trigger' rel='".$team."-".$i."-".$row['spelerId']."'" : "'").">".($row['rechten1'] & (1 << $i) ? "&#x44;" : "&#x46;")."</span></td>";	$i++;
					echo "<td><span class='icon_font".($wijzigen_toegestaan ? " selectable link_trigger' rel='".$team."-".$i."-".$row['spelerId']."'" : "'").">".($row['rechten1'] & (1 << $i) ? "&#x44;" : "&#x46;")."</span></td>";	$i++;
					echo "<td><span class='icon_font".($wijzigen_toegestaan ? " selectable link_trigger' rel='".$team."-".$i."-".$row['spelerId']."'" : "'").">".($row['rechten1'] & (1 << $i) ? "&#x44;" : "&#x46;")."</span></td>";	$i++;
					echo "<td><span class='icon_font".($wijzigen_toegestaan ? " selectable link_trigger' rel='".$team."-".$i."-".$row['spelerId']."'" : "'").">".($row['rechten1'] & (1 << $i) ? "&#x44;" : "&#x46;")."</span></td>";	$i++;
					echo "<td><span class='icon_font".($wijzigen_toegestaan ? " selectable link_trigger' rel='".$team."-".$i."-".$row['spelerId']."'" : "'").">".($row['rechten1'] & (1 << $i) ? "&#x44;" : "&#x46;")."</span></td>";	$i++;
					echo "<td><span class='icon_font".($wijzigen_toegestaan ? " selectable link_trigger' rel='".$team."-".$i."-".$row['spelerId']."'" : "'").">".($row['rechten1'] & (1 << $i) ? "&#x44;" : "&#x46;")."</span></td>";	$i++;
					echo "<td><span class='icon_font".($wijzigen_toegestaan ? " selectable link_trigger' rel='".$team."-".$i."-".$row['spelerId']."'" : "'").">".($row['rechten1'] & (1 << $i) ? "&#x44;" : "&#x46;")."</span></td>";	$i++;
				echo "</tr>";
				($color ? $color = false : $color = true);
			}
		echo "</body>";
		echo "</table>";
	}
}
echo "</div></div></div></div></div>";	//content_main, subcontent, content, submain, main
include_once($_SERVER['DOCUMENT_ROOT']."/footer.php");
?>